/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.behaviours.goal;

import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;

import java.io.IOException;

import ca.usherbrooke.agent.goal.BasicGoalAgent;
import ca.usherbrooke.agent.message.RequestMessageObjectGoal;
import ca.usherbrooke.behaviours.BasicRequestReponderBehaviour;
import ca.usherbrooke.behaviours.goal.request.RequestAddGoal.AddGoalInfo;
import ca.usherbrooke.goal.BasicGoal;
import ca.usherbrooke.goal.EntityGoal;

/**
 * @author Cody Stoutenburg
 * 
 */
public class GoalResponderBehaviour extends
		BasicRequestReponderBehaviour<BasicGoalAgent> {
	private static final long serialVersionUID = 1L;

	public final static String REQUEST_GOAL_PROTOCOL = "proto.request.goal";

	public final static MessageTemplate REQUEST_TEMPLATE = MessageTemplate.and(
			MessageTemplate.MatchProtocol(REQUEST_GOAL_PROTOCOL),
			MessageTemplate.MatchPerformative(ACLMessage.REQUEST));

	/**
	 * @param a
	 * @param mt
	 */
	public GoalResponderBehaviour(BasicGoalAgent a) {
		super(a, REQUEST_TEMPLATE, REQUEST_GOAL_PROTOCOL);
	}

	@Override
	protected ACLMessage prepareResult(ACLMessage request) {
		ACLMessage response = this.buildResultMessage(request);

		try {
			RequestMessageObjectGoal<?> message = (RequestMessageObjectGoal<?>) request
					.getContentObject();

			switch (message.getRequestType()) {
			case RequestMessageObjectGoal.REQUEST_ADD_GOAL:
				AddGoalInfo goalInfo = (AddGoalInfo) message.getValue();
				BasicGoal goal = null;
				try {
					goal = goalInfo.buildGoal();
					response.setContent("goal added");
				} catch (Exception ex1) {
					ex1.printStackTrace();
					goal = null;
				}
				if (goal != null) {
					this.getAgent().addGoal(goal);
				}
				break;
			case RequestMessageObjectGoal.REQUEST_REMOVE_GOAL:
				this.getAgent().removeGoal((BasicGoal) message.getValue());
				response.setContent("goal removed");
				break;
			case RequestMessageObjectGoal.REQUEST_GET_SCORE: {
				Long guid = (Long) message.getValue();
				Float score = this.getAgent().getScore(guid);
				try {
					response.setContentObject(score);
				} catch (IOException ex) {
					ex.printStackTrace();
				}
				break;
			}
			case RequestMessageObjectGoal.REQUEST_GET_ALL_ENTITY_GOALS: {
				Long guid = (Long) message.getValue();
				EntityGoal goals = this.getAgent().getEntityGoal(guid);
				try {
					response.setContentObject(goals);
				} catch (IOException ex) {
					ex.printStackTrace();
				}
				break;
			}
			default:
				System.out.println("unknow message receive");
				break;
			}
		} catch (UnreadableException ex) {
			ex.printStackTrace();
		}

		return response;
	}
}
